import $ from "jquery";
import router from '@/router' // 引入router
import Vue from 'vue';
import store from 'store';
import utils from '@/js/until.js';


import {setToken, scrollTop} from '@/libs/util'
// function setTop(){
//   if (document.getElementById('contentTop')) {
//     document.getElementById('contentTop').scrollTop = 0;
//   }
// }

// function getVuePro() {
//   return this;
// }

// 测试
// var test = 1;

const getLoading = utils.globleLoading();

var re = function () {
    return {
        ajax: function ajax(_ref) {
            var type = _ref.type,
                url = _ref.url,
                data = _ref.data,
                isAsync = _ref.isAsync,
                success = _ref.success,
                complete = _ref.complete,
                timeout = _ref.timeout || 10000,//瓒呮椂澶勭悊锛岄粯璁�10s
                error = _ref.error;

            if (!url) {
                console.error("璇疯緭鍏ヨ姹傚湴鍧€");
                return;
            }

            var xhr = new XMLHttpRequest();
            var that = this;
            var timeoutFlag = null;


            var query = [],
                queryData;

            for (var key in data) {
                query.push(encodeURIComponent(key) + "=" + encodeURIComponent(data[key]));
            }

            queryData = query.join("&");

            if (type == "GET") {
                if (url.indexOf("?") > -1) {
                    url = url + "&" + queryData;
                } else {
                    url = url + "?" + queryData;
                }
            }

            xhr.open(type || "GET", url, isAsync || true);

            xhr.onreadystatechange = function () {
                if (getLoading.loadingConfig.countLoading > 0) {
                    getLoading.loadingConfig.countLoading -= 1;
                }
                getLoading.hide();

                if (xhr.readyState == 4 && xhr.status == 200) {
                    window.clearTimeout(that.timeoutFlag);
                    success && success(JSON.parse(xhr.responseText));
                } else {
                    window.clearTimeout(that.timeoutFlag);

                    error
                }
            };
            if (type == "POST") {
                xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
                getLoading.show();

                xhr.send(query);

            } else {
                getLoading.show();

                xhr.send();

            }
            this.timeoutFlag = window.setTimeout(function () {//璁℃椂鍣紝瓒呮椂鍚庡鐞�
                window.clearTimeout(that.timeoutFlag);
                // removeLoad()
                xhr.abort();
            }.bind(this), timeout);
        }
    };
}();

export const ajax = function ajax(method, url, param) {

    return new Promise(function (resolve, reject) {

        re.ajax({
            type: method,
            url: url,
            timeout: 5000,
            data: param,
            success: function success(data) {
                if (Number(data.status) == 400) {
                }
                resolve(data);

            },
            error: function error(_error) {
                reject(_error);

            },
            complete: function completex(_error) {


            },
        });
    });
};